package com.zichen.pr.system.controller;

import com.zichen.pr.common.constant.UserConstants;
import com.zichen.pr.common.core.controller.BaseController;
import com.zichen.pr.common.core.domain.result.Result;
import com.zichen.pr.common.core.page.TableDataInfo;
import com.zichen.pr.system.entity.SysPost;
import com.zichen.pr.system.service.SysPostService;
import com.zichen.pr.util.DateUtils;
import com.zichen.pr.util.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: zichen
 * @description: 岗位 控制层
 * @author: rzhang
 * @create: 2021-04-23 08:10
 */
@RestController
@RequestMapping("/system/post")
public class SysPostController extends BaseController {

    @Resource
    private SysPostService sysPostService;

    /**
     * 获取岗位列表
     */
    @PreAuthorize("@ss.hasPermi('system:post:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysPost post) {
        startPage();
        List<SysPost> list = sysPostService.selectPostList(post);
        return getDataTable(list);
    }

    /**
     * 根据岗位编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:post:query')")
    @GetMapping(value = "/{postId}")
    public Result getInfo(@PathVariable Long postId) {
        return Result.success(sysPostService.selectPostById(postId));
    }

    /**
     * 新增岗位
     */
    @PreAuthorize("@ss.hasPermi('system:post:add')")
    @PostMapping
    public Result add(@Validated @RequestBody SysPost post) {
        if (UserConstants.NOT_UNIQUE.equals(sysPostService.checkPostNameUnique(post))) {
            return Result.error("新增岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(sysPostService.checkPostCodeUnique(post))) {
            return Result.error("新增岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        post.setCreateBy(SecurityUtils.getUsername());
        post.setCreateTime(DateUtils.getNowDate());
        return toAjax(sysPostService.save(post));
    }

    /**
     * 修改岗位
     */
    @PreAuthorize("@ss.hasPermi('system:post:edit')")
    @PutMapping
    public Result edit(@Validated @RequestBody SysPost post) {
        if (UserConstants.NOT_UNIQUE.equals(sysPostService.checkPostNameUnique(post))) {
            return Result.error("修改岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(sysPostService.checkPostCodeUnique(post))) {
            return Result.error("修改岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        post.setUpdateBy(SecurityUtils.getUsername());
        post.setUpdateTime(DateUtils.getNowDate());
        return toAjax(sysPostService.updateById(post));
    }

    /**
     * 删除岗位
     */
    @PreAuthorize("@ss.hasPermi('system:post:remove')")
    @DeleteMapping("/{postIds}")
    public Result remove(@PathVariable Long[] postIds) {
        return toAjax(sysPostService.deletePostByIds(postIds));
    }

    /**
     * 获取岗位选择框列表
     */
    @GetMapping("/optionselect")
    public Result optionselect() {
        List<SysPost> posts = sysPostService.selectPostAll();
        return Result.success(posts);
    }
}
